package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.dao.SaleListDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: jxc
 * @description:
 * @author: SXC
 * @create: 2021-09-04 14:48
 **/
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private SaleListDao saleListDao;

    /**
     * 获取数据
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @Override
    public List<Customer> customerList(Integer page, Integer rows, String customerName) {
        page  = page < 1 ? 0 : (page -1)*rows;
        rows  = rows < 1 ? 10 : rows;
        return customerDao.selectCustomerList(page, rows, customerName);
    }

    /**
     * 获取条数
     * @return
     */
    @Override
    public int selectCount() {
        return customerDao.selectCount();
    }

    /**
     * 2.2、客户添加或修改
     * @param customer
     */
    @Override
    public void saveOrUpdateCustomer(Customer customer) {
        if (customer != null){
            if (customer.getCustomerId() != null){
                //修改
                customerDao.updateCustomer(customer);
            }else {
                //添加
                customerDao.saveCustomer(customer);
            }
        }
    }

    /**
     * 客户删除（支持批量删除）
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteList(String[] ids) {
        if (ids != null){
            for (String id : ids) {
                customerDao.delete(id);
                customerDao.delete(id);
            }
        }
    }
}
